#include "calculator.h"
#include "ui_calculator.h"
#include<Widget.h>

Calculator::Calculator(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Calculator)
{
    ui->setupUi(this);
    this->setWindowTitle("Chen 20240430");
    QFont font1("仿宋",14);
    ui->mainLineEdit->setFont(font1);
    QIcon deltCon(":/Calculator/images/rsc/deltBotton.png");
   ui->deltButton->setIcon(deltCon);


    ui->equalButton->setStyleSheet("background:yellow");


}

Calculator::~Calculator()
{
    delete ui;
}

void Calculator::on_oneButton_clicked()
{
    expression+="1";
    ui->mainLineEdit->setText(expression);
}
void Calculator::on_twoButton_clicked()
{
    expression+="2";
    ui->mainLineEdit->setText(expression);
}
void Calculator::on_threeButton_clicked()
{
    expression+="3";
    ui->mainLineEdit->setText(expression);
}
void Calculator::on_fourButton_clicked()
{
    expression+="4";
    ui->mainLineEdit->setText(expression);
}
void Calculator::on_fiveButton_clicked()
{
    expression+="5";
    ui->mainLineEdit->setText(expression);
}
void Calculator::on_sixButton_clicked()
{
    expression+="6";
    ui->mainLineEdit->setText(expression);
}
void Calculator::on_sevenButton_clicked()
{
    expression+="7";
    ui->mainLineEdit->setText(expression);
}
void Calculator::on_eightButton_clicked()
{
    expression+="8";
    ui->mainLineEdit->setText(expression);
}
void Calculator::on_nineButton_clicked()
{
    expression+="9";
    ui->mainLineEdit->setText(expression);
}
void Calculator::on_zeroButton_clicked()
{
    expression+="0";
    ui->mainLineEdit->setText(expression);
}
void Calculator::on_leftButton_clicked()
{
    expression+="(";
    ui->mainLineEdit->setText(expression);
}
void Calculator::on_rightButton_clicked()
{
    expression+=")";
    ui->mainLineEdit->setText(expression);
}
void Calculator::on_addButton_clicked()
{
    expression+="+";
    ui->mainLineEdit->setText(expression);
}
void Calculator::on_subButton_clicked()
{
    expression+="-";
    ui->mainLineEdit->setText(expression);
}
void Calculator::on_multButton_clicked()
{
    expression+="*";
    ui->mainLineEdit->setText(expression);
}
void Calculator::on_divButton_clicked()
{
    expression+="/";
    ui->mainLineEdit->setText(expression);
}

void Calculator::on_clearButton_clicked()
{
    expression.clear();
    ui->mainLineEdit->clear();
}
void Calculator::on_deltButton_clicked()
{
    expression.chop(1);
    ui->mainLineEdit->setText(expression);
}
void Calculator::on_equalButton_clicked()
{
    QStack <int>s_num;
    QStack<char>s_opt;

    char opt[128]={0};
    int i=0,tmp=0;
    QByteArray ba=expression.toUtf8();


    strcpy(opt,ba.data());


    while(opt[i]!='\0'||s_opt.empty()!=true)
    {
        if(opt[i]>='0'&&opt[i]<='9')
        {
            tmp=tmp*10+opt[i]-'0';
            i++;
            if(opt[i]<'0'||opt[i]>'9')
            {
                s_num.push(tmp);
                tmp=0;

            }
        }
        else
        {

            if(s_opt.empty()==true||Priority(opt[i])>Priority(s_opt.top())||(s_opt.top()=='('&&opt[i]!=')'))
            {
                s_opt.push(opt[i]);
                i++;
                continue;
            }
            if(s_opt.top()=='('&&opt[i]==')')
            {
                s_opt.pop();
                i++;
                continue;
            }
            if(Priority(opt[i])<=Priority(s_opt.top())||(opt[i]==')'&&s_opt.top()!='(')||(opt[i]=='\0'&&s_opt.empty()!=true))
            {
                char ch=s_opt.top();
                s_opt.pop();
                int num2=s_num.top();
                s_num.pop();
                int num1=s_num.top();
                s_num.pop();

                switch (ch) {
                case '+':
                    s_num.push(num1+num2);
                    break;
                case '-':
                    s_num.push(num1-num2);
                    break;
                case '*':
                    s_num.push(num1*num2);
                    break;
                case '/':
                    if (num2 != 0) { // 检查除数是否为零
                        s_num.push(num1 / num2);
                    } else {
                        // 处理除数为零的情况
                        // 例如，可以显示一个错误消息
                        QMessageBox::warning(this, "Error", "Division by zero!");
                        return; // 结束函数执行
                    }
                    break;
                }




            }


        }




    }

    if (!s_num.isEmpty()) {
        ui->mainLineEdit->setText(QString::number(s_num.top()));
    } else {

        ui->mainLineEdit->setText("Error: Invalid expression");
    }
    expression.clear();
}
int Calculator::Priority(char ch)
{
    switch(ch)
    {
    case '(':
        return 3;
    case '*':
    case '/':
        return 2;
    case '+':
    case '-':
        return 1;
    default:
        return 0;
    }

}

void Calculator::on_backButton_clicked()
{
    this->hide();
    Widget *w=new Widget;
    w->show();
};
